﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BangumiWpfApp1
{
    /// <summary>
    /// PullView.xaml 的交互逻辑
    /// </summary>
    public partial class PullView : Window
    {
        private int startId;
        private int stopId;
        private string logFile;
        private int index = 0;
        private Stopwatch stopwatch = new Stopwatch();
        public PullView()
        {
            InitializeComponent();
            this.Loaded += PullView_Loaded;
        }

        private void PullView_Loaded(object sender, RoutedEventArgs e)
        {
            this.Title = this.Title + $" {startId}  ->  {stopId}";
            BangumiApi bangumiApi = new BangumiApi();
            bangumiApi.pull += BangumiApi_pull;
            Task.Run(()=> 
            {
                stopwatch.Start();
                bangumiApi.download(startId, stopId);
                stopwatch.Stop();
                File.AppendAllText(logFile, $"查询耗时  {stopwatch.ElapsedMilliseconds / 1000 } 秒\r\n");
            });
           
        }

        private string sec2str(double s) 
        {
            if (s < 60)
            {
                return s+"秒";
            }
            else 
            {
                if (s < 3600)
                {
                    int fen = (int)(s / 60);
                    double miao = s - fen * 60;
                    return $"{fen}分钟{miao.ToString("f1")}秒 ";
                }
                else 
                {
                    int hour = (int)(s / 3600);

                    double s2 = s - hour * 3600;
                    int fen = (int)(s2 / 60);
                    double miao = s2 - fen * 60;
                    return $"{hour}小时{fen}分钟{miao.ToString("f1")}秒 ";
                }
            }
          

        }
        private void BangumiApi_pull(object sender, Subject e)
        {
            index++;
            int count = (stopId - startId) + 1;
            double p = (double)(index) / (count);
            double pro = (double)(index) / (count) * 100;
            double sec = stopwatch.ElapsedMilliseconds / 1000;
            double left = sec / p - sec;
            string proStr = $"{pro.ToString("f2")}     {index} / {count}   耗时:{sec2str(sec)} 剩余:{sec2str(left)}秒 ";


            string log = $"{index}";
            Subject subj = e;
            if (subj!=null)
            {
                log = $"{subj.type} {subj.id} {subj.name} {subj.name_cn} {subj.air_date}";
            }           
            this.Dispatcher.Invoke(()=>
            {
                if (logListBox.Items.Count >18)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        logListBox.Items.RemoveAt(0);                       
                    }                   
                }
                logListBox.Items.Add(log);
                File.AppendAllText(logFile, $"{log}\r\n");
                progressTextBlock.Text = proStr;
                this.progress.Value = pro;
            });
        }

        internal void setTask(int startId, int stopId)
        {
            this.startId = startId;
            this.stopId = stopId;
            logFile = AppDomain.CurrentDomain.BaseDirectory + $"{startId}-{stopId}.log";
        }
    }
}
